package org.jtb.quickr;

import java.net.URL;
import java.util.Properties;
import org.apache.log4j.Logger;

/**
 * Quickr Auth service.
 */
public class AuthService extends Service {
    private static Logger logger = Logger.getLogger(AuthService.class.getName());
    
    /**
     * Construct a new quickr auth service. Load quickr.properties from the class path.
     * @throws QuickrException If there was a problem constructing this object.
     */
    public AuthService() throws QuickrException {
        super();
    }
    
    /**
     * Construct a new quickr auth service based on the given properties.
     * @param properties The quickr properties.
     * @throws QuickrException If a problem occurs constructing this object.
     */
    public AuthService(Properties properties) throws QuickrException {
        super(properties);
    }
    
    /**
     * Call the flick.auth.getFrob method.
     * @return The frob.
     * @throws QuickrException If a problem occurs getting the frob.
     */
    public FrobResponse getFrob() throws QuickrException {
        AuthGetFrobRequest gfreq = new AuthGetFrobRequest(properties.getRestUrl(), properties.getApiKey(), properties.getSecret());
        FrobResponse frob = gfreq.execute();
        
        if (logger.isDebugEnabled()) {
            logger.debug("frob: " + frob);
        }
        
        return frob;
    }
        
    /**
     * Get a desktop authentication URL.
     * @param frob The frob.
     * @param perms The permission to request.
     * @return The authentication URL.
     * @throws QuickrException If there was a problem getting the URL.
     */
    public URL getAuthUrl(FrobResponse frob, Permissions perms) throws QuickrException {
        FlickrUrl furl = new AuthUrl(properties.getAuthUrl(), properties.getApiKey(), properties.getSecret(), frob, perms);
        return furl.getUrl();
    }
    
    /**
     * Get a web authentication URL.
     * @param perms The permission to request.
     * @return The authentication URL.
     * @throws QuickrException If there was a problem getting the URL.
     */
    public URL getAuthUrl(Permissions perms) throws QuickrException {
        FlickrUrl furl = new AuthUrl(properties.getAuthUrl(), properties.getApiKey(), properties.getSecret(), perms);
        return furl.getUrl();
    }
    
    
    /**
     * Call the flickr.auth.getToken method.
     * @param frob The frob to get a token for.
     * @return The token.
     * @throws QuickrException If a problem occurs getting the token.
     */
    public AuthResponse getToken(FrobResponse frob) throws QuickrException {
        if (frob == null) {
            throw new NullPointerException("frob cannot be null");
        }
        return getToken(frob.toString());
    }
    
    /**
     * Call the flickr.auth.getToken method.
     * @param frobString The frob to get a token for, in string form.
     * @return The token.
     * @throws QuickrException If a problem occurs getting the token.
     */
    public AuthResponse getToken(String frobString) throws QuickrException {
        if (frobString == null) {
            throw new NullPointerException("frob cannot be null");
        }
        AuthGetTokenRequest gtreq = new AuthGetTokenRequest(properties.getRestUrl(), properties.getApiKey(), properties.getSecret(), frobString);
        AuthResponse ares = gtreq.execute();
        
        return ares;
    }
    
    /**
     * Call the flickr.auth.checkToken method.
     * @param authToken The token to check.
     * @return The authentication response. If a value is returned, then the token is valid.
     * @throws QuickrException If a problem occurs checking the token or if the token is invalid.
     */
    public AuthResponse checkToken(AuthResponse.Token authToken) throws QuickrException {
        AuthCheckTokenRequest ctreq = new AuthCheckTokenRequest(properties.getRestUrl(), properties.getApiKey(), properties.getSecret(), authToken);
        AuthResponse ares = ctreq.execute();
        
        return ares;
    }
}
